<template>
  <div>
    <!-- 三级分类 -->
    <Category :scene="scene"></Category>
    <el-card>
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="plus"
          :disabled="!categoryStore.c3Id"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <!-- 展示数据 -->
        <el-table border :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80"
          ></el-table-column>
          <el-table-column
            label="名称"
            align="center"
            prop="spuName"
          ></el-table-column>
          <el-table-column
            label="描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="操作">
            <template #="{ row, $index }">
              <el-button
                color="#9CD7FF"
                size="small"
                title="添加"
                icon="plus"
                @click="addSku(row)"
              ></el-button>
              <el-button
                color="#529b2e"
                size="small"
                title="修改"
                icon="edit"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                color="#FFEB9C"
                size="small"
                title="查看"
                icon="view"
                @click="findSku(row)"
              ></el-button>
              <el-popconfirm
                :title="`确认删除${row.spuName}?`"
                width="200px"
                @confirm="deleteSpu(row)"
              >
                <template #reference>
                  <el-button
                    color="#F56C6C"
                    size="small"
                    title="删除"
                    icon="delete"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:currentPage="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper, -> , sizes, total"
          :total="total"
          @current-change="getSpu"
          @size-change="sizeChage"
        ></el-pagination>
      </div>
      <!-- 添加SPU|修改SPU子组件 -->
      <SpuForm ref="spu" v-show="scene == 1" @changeScene="changeScene" />
      <!-- 添加SKU子组件 -->
      <SkuForm ref="sku" v-show="scene == 2" @changeScene="changeScene" />
      <!-- 对话框展示已有的SKU数据 -->
      <el-dialog v-model="show" title="SKU列表">
        <el-table border :data="skuArr">
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格" prop="price"></el-table-column>
          <el-table-column label="SKU重量" prop="weight"></el-table-column>
          <el-table-column label="SKU图片">
            <template #="{ row, $index }">
              <img :src="row.skuDefaultImg" width="100px" alt="" />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from 'vue'
import { reqSPU, reqSkuList, reqRemoveSpu } from '@/api/product/spu'
import type {
  SPUResponseData,
  Records,
  SpuData,
  SkuInfoData,
  SkuData,
} from '@/api/product/spu/type'
// 引入子组件
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'
// 引入分类的仓库
import useCategoryStore from '@/store/modules/category'
import { ElMessage } from 'element-plus'

let categoryStore = useCategoryStore()
// 场景的数据
let scene = ref<number>(0) // 0 显示已有SPU 1 添加或修改已有SPU 2 修改SKU的结构
// 分页器默认页码
let pageNo = ref<number>(1)
// 每页展示的数据
let pageSize = ref<number>(3)
// SPU总个数
let total = ref<number>(0)
// 存储已有的SPU的数据
let records = ref<Records>([])
// 获取子组件实例
let spu = ref<any>()
let sku = ref<any>()
// 存储全部的SKU数据
let skuArr = ref<SkuData[]>([])
let show = ref<boolean>(false)
// 监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  () => {
    //当三级分类发生变化的时候清空对应的数据
    records.value = []
    if (!categoryStore.c3Id) return
    getSpu()
  },
)

// 方法执行，获取某个三级分类下全部的SPU
const getSpu = async (pager = 1) => {
  // 修改当前页码
  pageNo.value = pager
  let result: SPUResponseData = await reqSPU(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
// 分页器下拉菜单变化事件
const sizeChage = () => {
  getSpu()
}
// 添加SPU按钮的回调
const addSpu = () => {
  // 切换为SPU结构
  scene.value = 1
  // 调用子组件初始化方法
  spu.value.initAddSpu(categoryStore.c3Id)
}
// 修改SPU按钮的回调
const updateSpu = (row: SpuData) => {
  // 切换为SPU结构
  scene.value = 1
  // 调用子组件实例方法
  spu.value.initSpuData(row)
}
// 子组件SPUform绑定自定义事件
const changeScene = (obj: any) => {
  // 子组件spuform点击取消切换spu
  scene.value = obj.flag
  if (obj.params == 'update') {
    // 更新留在当前页
    getSpu(pageNo.value)
  } else {
    getSpu()
  }
}
// 添加SKU按钮的回调
const addSku = (row: SpuData) => {
  scene.value = 2
  // 调用子组件的方法初始化添加SKU的数据
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}
// 查看SKU列表的数据
const findSku = async (row: SpuData) => {
  let result: SkuInfoData = await reqSkuList(row.id as number)
  if (result.code == 200) {
    skuArr.value = result.data
    show.value = true
  }
}
// 删除SPU按钮的回调
const deleteSpu = async (row: SpuData) => {
  let result = await reqRemoveSpu(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    // 获取SPU数据
    getSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
onBeforeUnmount(() => {
  // 清空仓库的数据
  categoryStore.$reset()
})
</script>

<style scoped></style>
